//
// Copyright (c) 2010 xiaoningyb (xiaoningyb@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <boost/bind.hpp>
#include <iostream>
#include <vector>

#include "sharing_connection.h"
#include "sharing_client.h"

namespace tinysharing
{
    client::client(boost::asio::io_service& io_service,     
                   const std::string& host, const std::string& service)
        : connection_(io_service, reply_handler_)
    {
        // Resolve the host name into an IP address.
        boost::asio::ip::tcp::resolver resolver(io_service);
        boost::asio::ip::tcp::resolver::query query(host, service);
        boost::asio::ip::tcp::resolver::iterator endpoint_iterator =
            resolver.resolve(query);
        boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;

        // Start an asynchronous connect operation.
        connection_.socket().async_connect(endpoint,
                                           boost::bind(&client::handle_connect,
                                                       this,
                                                       boost::asio::placeholders::error,
                                                       ++endpoint_iterator));
    }

    /// Handle completion of a connect operation.
    void client::handle_connect(const boost::system::error_code& e,
                                boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
    {
        if (!e)
        {
            // Successfully established connection. Start operation to read the list
            // of stocks. The connection::async_read() function will automatically
            // decode the data that is read from the underlying socket.
            void (client::*f)(const boost::system::error_code&) = &client::handle_read;
            connection_.async_read(message_, boost::bind(f, this,
                                                       boost::asio::placeholders::error));
        }
        else if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator())
        {
            // Try the next endpoint.
            connection_.socket().close();
            boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
            connection_.socket().async_connect(endpoint,
                                               boost::bind(&client::handle_connect,
                                                           this,
                                                           boost::asio::placeholders::error,
                                                           ++endpoint_iterator));
        }
        else
        {
            // An error occurred. Log it and return. Since we are not starting a new
            // operation the io_service will run out of work to do and the client will
            // exit.
            std::cerr << e.message() << std::endl;
        }
    }

    /// Handle completion of a read operation.
    void client::handle_read(const boost::system::error_code& e)
    {
        if (!e)
        {
            std::cout<<"read the data"<<std::endl;            
        }
        else
        {
            // An error occurred.
            std::cerr << e.message() << std::endl;
        }

        // Since we are not starting a new operation the io_service will run out of
        // work to do and the client will exit.
    }
    
} // namespace tinysharing
